// pages/addAddress/addAddress.js

var app = getApp();
var page=1;
var selectedNews = false;
var newsList = [];
var haveTap = 0 ;// 屏蔽重复点击
Page({
  data:{
    addrId:'',
    phone: '',
    consignee: '',
    address: '',
    area: '',
    is_default: 2,
    isChecked1:false,
    isedit: false,
    addresslist: [],
    region: [],
    goodsId: [],
    mount: [],
    backType: "",
    gotype: "",
    
  },
  inputgetName: function (e) {
    this.setData({
      consignee: e.detail.value
    });
  },
  inputgetPhone: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
  inputgetAddress: function (e) {
    this.setData({
      address: e.detail.value
    });
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },
  //点击设置默认地址
  changeSwitch1: function (e) {
    var value = e.detail.value;
    if(value == true){
      this.data.is_default = 1;
    }else{
      this.data.is_default = 2;
    }
    this.setData({
      isChecked1: value
    });
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数

    this.setData({
      backType: options.backType,
      gotype: options.gotype,
      
    })
    if (!!options.goodsId) {
      this.setData({
        goodsId: options.goodsId,
        mount: options.mount,
        spec_price_id: options.spec_price_id
      })
    }
    if (!!options.info) {
      var addr = JSON.parse(options.info);
      console.log(options)
      var isdefault = options.isdefault;
      if (isdefault == 1){
        var isChecked1 = true;
      }else{
        isChecked1 = false;
      }
      var area = addr.area.split(' ');
      var region = []; 
      region.push(area[0]);
      region.push(area[1]);
      region.push(area[2]);
      this.setData({
        addrId: addr.id,
        phone: addr.phone,
        consignee: addr.consignee,
        area: addr.area,
        address: addr.address,
        region: region,
        is_default: isdefault,
        isChecked1: isChecked1,
        isedit: true
      })
    } else {
      this.setData({
        isedit: false
      })
    }

  },
  //点击删除地址
  bindDeleteTap: function (e) {
    var url2 = '/address/del';
    var params2 = {
      id: this.data.addrId
    };
    var that = this;
    app.HttpGetSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        
        if (JSON.parse(wx.getStorageSync('addrinfo')).id == that.data.addrId){
          console.log('删除');
          wx.removeStorageSync('addrinfo')
        }
        app.showTipMsg('删除地址成功');
        wx.navigateBack({
          url: '../myAddress/myAddress?goodsId=' + that.data.goodsId + '&mount=' + that.data.mount + '&backType=' + that.data.backType + '&gotype=' + that.data.gotype + '&spec_price_id=' + that.data.spec_price_id,
        })
      }
    });
  },
  //点击保存编辑地址
  bindEditTap: function (e) {
    var that = this;
    if (haveTap == 1) {

    } else {
      haveTap = 1;
      this.data.area = '';
      for (let i = 0; i < this.data.region.length; i++) {
        if (i == this.data.region.length - 1) {
          this.data.area += this.data.region[i];
        } else {
          this.data.area += this.data.region[i] + ' ';
        }
      }
      var patrn = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/im;
      if (patrn.test(this.data.address)) {// 如果包含特殊字符返回false
        wx.showModal({
          title: '提示',
          content: '请勿添加特殊符号',
          showCancel: false,
          success: function () { }
        })
        haveTap = 0;
        return false;
      }

      var url2 = '/address/edit';
      var params2 = {
        uid: app.globalData.userInfo.id,
        id: this.data.addrId,
        phone: this.data.phone,
        consignee: this.data.consignee,
        area: this.data.area,
        address: this.data.address,
        is_default: this.data.is_default,
      };
      var that = this;
      app.HttpPostSend(url2, params2, function (res) {
        if (res.msg != "success") {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
            success: function () { }
          })
          haveTap = 0;
          return false;
          // app.showTipMsg(res.msg);
        } else {
          app.showTipMsg('修改地址成功');
          wx.navigateBack({
            url: '../myAddress/myAddress?goodsId=' + that.data.goodsId + '&mount=' + that.data.mount + '&backType=' + that.data.backType + '&gotype=' + that.data.gotype + '&spec_price_id=' + that.data.spec_price_id,
          })
        }
      });
    }
    
  },
  //点击保存新增地址
  bindSaveTap: function (e) {
    var that = this;
    if (haveTap==1){

    }else{
      haveTap = 1;
      this.data.area = '';
      for (let i = 0; i < this.data.region.length; i++) {
        if (i == this.data.region.length - 1) {
          this.data.area += this.data.region[i];
        } else {
          this.data.area += this.data.region[i] + ' ';
        }
      }

      var patrn = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/im;
      if (patrn.test(this.data.address)) {// 如果包含特殊字符返回false
        wx.showModal({
          title: '提示',
          content: '请勿添加特殊符号',
          showCancel: false,
          success: function () { }
        })
        haveTap = 0;
        return false;
      }

      var url2 = '/address/add';
      var params2 = {
        uid: app.globalData.userInfo.id,
        phone: this.data.phone,
        consignee: this.data.consignee,
        area: this.data.area,
        address: this.data.address,
        is_default: this.data.is_default,
      };
      var that = this;
      app.HttpPostSend(url2, params2, function (res) {
        if (res.msg != "success") {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
            success: function () { }
          })
          haveTap = 0;
          return false;
        } else {
          app.showTipMsg('新增地址成功');

          // 雷达添加查看记录 -登记收货地址数
          app.radarAdd(app.globalData.SwitchTabParams.v_id, 17,'','',app.globalData.userInfo.logo)

          wx.navigateBack({
            url: '../myAddress/myAddress?goodsId=' + that.data.goodsId + '&mount=' + that.data.mount + '&backType=' + that.data.backType + '&gotype=' + that.data.gotype + '&spec_price_id=' + that.data.spec_price_id,
          })
        }
      });
    }
    
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    haveTap = 0;
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // onShareAppMessage: function () {
    
  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    
  }
})